<?php
class Workflow_Execution_DatabasePlugin extends Workflow_Execution_Plugin
{
	/**
     * Called after an execution has been started.
     *
     * @param Workflow_Execution_Database $execution
     */
    public function afterExecutionStarted( Workflow_Execution_Database $execution )
    {
    	$principal = new Ext_Service_Principal();
    	$identity = $principal->getIdentity();
    	$userid = $identity->id;
    	
    	$db = $execution->getDb();
    	$db->insert('execution_histroy', array(
    		'workflow_id' => $execution->workflow->id,
    		'execution_id' => $execution->getId(),
    		'start_user' => $userid,
    		'start_time' => time()
    	));
    }
    // @codeCoverageIgnoreEnd

    /**
     * Called after an execution has been suspended.
     *
     * @param Workflow_Execution_Database $execution
     */
    public function afterExecutionSuspended( Workflow_Execution_Database $execution )
    {
    // @codeCoverageIgnoreStart
    }
    // @codeCoverageIgnoreEnd

    /**
     * Called after an execution has been resumed.
     *
     * @param Workflow_Execution_Database $execution
     */
    public function afterExecutionResumed( Workflow_Execution_Database $execution )
    {
    // @codeCoverageIgnoreStart
    }
    // @codeCoverageIgnoreEnd

    /**
     * Called after an execution has been cancelled.
     *
     * @param Workflow_Execution_Database $execution
     */
    public function afterExecutionCancelled( Workflow_Execution_Database $execution )
    {
    	$db = $execution->getDb();
    	$db->update('execution_histroy', array(
    		'isCanceled' => 1,
    		'canceldtime' => time(),
    		'execution_variables' => Workflow_DatabaseUtil::serialize( $execution->getVariables() ),
    	), array(
    		'workflow_id = ?' => $execution->workflow->id,
    		'execution_id = ?' => $execution->getId()
    	));
    }
    // @codeCoverageIgnoreEnd

    /**
     * Called after an execution has successfully ended.
     *
     * @param Workflow_Execution_Database $execution
     */
    public function afterExecutionEnded( Workflow_Execution_Database $execution )
    {
   		$db = $execution->getDb();
    	$db->update('execution_histroy', array(
    		'isCompleted' => 1,
    		'completedtime' => time(),
    		'execution_variables' => Workflow_DatabaseUtil::serialize( $execution->getVariables() ),
    	), array(
    		'workflow_id = ?' => $execution->workflow->id,
    		'execution_id = ?' => $execution->getId()
    	));
    }
    // @codeCoverageIgnoreEnd

    /**
     * Called before a node is activated.
     *
     * @param Workflow_Execution_Database $execution
     * @param Workflow_Node      $node
     * @return bool true, when the node should be activated, false otherwise
     */
    public function beforeNodeActivated( Workflow_Execution_Database $execution, Workflow_Node $node )
    {
    // @codeCoverageIgnoreStart
        return true;
    }
    // @codeCoverageIgnoreEnd

    /**
     * Called after a node has been activated.
     *
     * @param Workflow_Execution_Database $execution
     * @param Workflow_Node      $node
     */
    public function afterNodeActivated( Workflow_Execution_Database $execution, Workflow_Node $node )
    {
    // @codeCoverageIgnoreStart
    }
    // @codeCoverageIgnoreEnd

    /**
     * Called after a node has been executed.
     *
     * @param Workflow_Execution_Database $execution
     * @param Workflow_Node      $node
     */
    public function afterNodeExecuted( Workflow_Execution_Database $execution, Workflow_Node $node )
    {
    	if ($node instanceof  Workflow_Node_UserTask)
    	{
    		$principal = new Ext_Service_Principal();
    		$identity = $principal->getIdentity();
    		$userid = $identity->id;
    	
    		$db = $execution->getDb();
    		$db->insert('execution_history_records', array(
    			'execution_id' => $execution->getId(),
    			'workflow_id' => $execution->workflow->id,
    			'node_id' => $node->getId(),
    			'user_id' => $userid,
    			'optime' => time()
    		));
    	}
    }
    // @codeCoverageIgnoreEnd

    /**
     * Called after a new thread has been started.
     *
     * @param Workflow_Execution_Database $execution
     * @param int                  $threadId
     * @param int                  $parentId
     * @param int                  $numSiblings
     */
    public function afterThreadStarted( Workflow_Execution_Database $execution, $threadId, $parentId, $numSiblings )
    {
    // @codeCoverageIgnoreStart
    }
    // @codeCoverageIgnoreEnd

    /**
     * Called after a thread has ended.
     *
     * @param Workflow_Execution_Database $execution
     * @param int                  $threadId
     */
    public function afterThreadEnded( Workflow_Execution_Database $execution, $threadId )
    {
    // @codeCoverageIgnoreStart
    }
    // @codeCoverageIgnoreEnd

    /**
     * Called before a variable is set.
     *
     * @param  Workflow_Execution_Database $execution
     * @param  string               $variableName
     * @param  mixed                $value
     * @return mixed the value the variable should be set to
     */
    public function beforeVariableSet( Workflow_Execution_Database $execution, $variableName, $value )
    {
    // @codeCoverageIgnoreStart
        return $value;
    }
    // @codeCoverageIgnoreEnd

    /**
     * Called after a variable has been set.
     *
     * @param Workflow_Execution_Database $execution
     * @param string               $variableName
     * @param mixed                $value
     */
    public function afterVariableSet( Workflow_Execution_Database $execution, $variableName, $value )
    {
    // @codeCoverageIgnoreStart
    }
    // @codeCoverageIgnoreEnd

    /**
     * Called before a variable is unset.
     *
     * @param  Workflow_Execution_Database $execution
     * @param  string               $variableName
     * @return bool true, when the variable should be unset, false otherwise
     */
    public function beforeVariableUnset( Workflow_Execution_Database $execution, $variableName )
    {
    // @codeCoverageIgnoreStart
        return true;
    }
    // @codeCoverageIgnoreEnd

    /**
     * Called after a variable has been unset.
     *
     * @param Workflow_Execution_Database $execution
     * @param string               $variableName
     */
    public function afterVariableUnset( Workflow_Execution_Database $execution, $variableName )
    {
    // @codeCoverageIgnoreStart
    }
    // @codeCoverageIgnoreEnd	
}
?>